package dk.nordahl.cameratest;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {



	private static final int TAKE_AVATAR_GALLERY_REQUEST = 0;
	private static final int TAKE_AVATAR_CAMERA_REQUEST = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
		
	public void findPic (View v)
	{
		Button btn = (Button)findViewById(R.id.goToGallery);
		
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent takePhoto = new Intent(Intent.ACTION_PICK);
				takePhoto.setType("image/*");
				startActivityForResult(Intent.createChooser(takePhoto, "Select picture"),TAKE_AVATAR_GALLERY_REQUEST);
				
			}
		});
		
		
	}
	
	public void takePic (View view)
	{
		Button btn = (Button)findViewById(R.id.goToCamera);
		
		btn.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
			Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(Intent.createChooser(pictureIntent,"Take Picture"), TAKE_AVATAR_CAMERA_REQUEST);
			return true;
			}
			});

			
	}
	
	@Override
	protected void onActivityResult(int requestCode, int retCode, Intent data)
	{
		switch (requestCode) {
		case TAKE_AVATAR_GALLERY_REQUEST:
			if(retCode ==Activity.RESULT_CANCELED)
			{
				
			}
			else if(retCode == Activity.RESULT_OK)
			{
				Uri photoUri = data.getData();
				if(photoUri != null)
				{
					try
					{
						ImageView img = (ImageView)findViewById(R.id.imageView1);
						img.setImageURI(photoUri);
					}
					catch(Exception e)
					{
						Log.e("Exception", "Could not load image");
					}
			}
			}
			break;
			case TAKE_AVATAR_CAMERA_REQUEST:
            if (requestCode == Activity.RESULT_CANCELED)
            {
            } 
            else if (requestCode == Activity.RESULT_OK)
            {
                Bitmap cameraPic = (Bitmap) data.getExtras().get("data");
                if (cameraPic != null) 
                {
                    try 
                    {
                        ImageView img=(ImageView)findViewById(R.id.imageView1);
                        img.setImageBitmap(cameraPic);
                    } 
                    catch (Exception e)
                    {
                        Log.e("Error:", "Camera image failed.", e);
                    }
                }
            }
            break;

		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
